package com.victor.zjbj_android.yinhuan;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.victor.zjbj_android.R;
import com.victor.zjbj_android.utils.LoginUtils;
import com.victor.zjbj_android.utils.PhotoPopupManager;
import com.victor.zjbj_android.utils.PhotoUtils;
import com.victor.zjbj_android.yinhuan.fragment.YinhuanchuliFragment;

import java.io.IOException;
import java.util.ArrayList;

public class NewYinhuanActivity extends AppCompatActivity {
    private TextView img_text;
    private TextView tv_photo;
    private TextView tv_yhtype;
    private TextView tv_author;
    private RelativeLayout rl_yhlx;
    private RelativeLayout rl_xcwz;
    private RelativeLayout rl_xxdz;
    private RelativeLayout rl_fxr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_yinhuan);
        initViews();
    }

    private void initViews() {
        tv_yhtype=findViewById(R.id.tv_yhtype);
        tv_author=findViewById(R.id.tv_author);
        img_text=findViewById(R.id.img_text);
        tv_photo=findViewById(R.id.tv_photo);
        rl_yhlx=findViewById(R.id.rl_yhlx);
        rl_xcwz=findViewById(R.id.rl_xcwz);
        rl_xxdz=findViewById(R.id.rl_xxdz);
        rl_fxr=findViewById(R.id.rl_fxr);
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                forceHideIM();
                PhotoPopupManager.getInstance().show(NewYinhuanActivity.this);
            }
        });

        // 获取图片
        PhotoPopupManager.getInstance().setOnPhotoSelectedListener(new PhotoPopupManager.OnPhotoSelectedListener() {
            @Override
            public void onPhotoSelected(String photoPath) {
                if (!TextUtils.isEmpty(photoPath)) {
                    try {
                        String tartImg = PhotoUtils.copyImg2Target(NewYinhuanActivity.this, photoPath);
                        if (!TextUtils.isEmpty(tartImg)) {
                            ToastUtils.showLong(tartImg);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        rl_yhlx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(new Intent(NewYinhuanActivity.this,YiHuanTypeActivity.class), 1);
            }
        });
        rl_xcwz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(NewYinhuanActivity.this,NewAdressActivity.class));
            }
        });
        findViewById(R.id.btn_chuli).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(NewYinhuanActivity.this, YinHuanChuLiActivity.class));
            }
        });

        tv_author.setText(LoginUtils.getLoginConfig(getApplicationContext()).getUsername());

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1)
        {
            tv_yhtype.setText(data.getExtras().getString("result_str"));
        }
        else
        {
            PhotoPopupManager.getInstance().onActivityResulted(this, requestCode, resultCode, data);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
//    private void setAdapter() {
//        GridLayoutManager layoutManager = new GridLayoutManager(NewYinhuanActivity.this, 3);
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        adapter = new BaoXiaoImgAdapter(R.layout.item_baoxiao_img, NewYinhuanActivity.this);
//        img_recy.setLayoutManager(layoutManager);
//        img_recy.setAdapter(adapter);
//        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ArrayList<String> arrayList=new ArrayList<>();
//                for (int i=0;i<imglist.size();i++){
//                    String a=imglist.get(i).getFile_path();
//                    arrayList.add(a);
//                }
//                ImageDetailActivity.actionStart(NewYinhuanActivity.this, arrayList, position);
//            }
//        });
//
//    }
    /**
     * 强制隐藏键盘
     */
    public void forceHideIM() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager == null) {
            return;
        }
        View decorView = getWindow().getDecorView();
        inputMethodManager.hideSoftInputFromWindow(decorView.getWindowToken(), 0);
    }
}
